/* 启动时加载 */
/*
 */
var S = 0, D = 0, H = 0, Y = 0, G = 0, W = 0;
var etcd = 0, shebeiChars = undefined;
var maxAjax = 1080, nowAjax = 0;
(function () {

	hsmCovMis = function (time){
		if( time == 0 ) return 0;
		var hour = time.split(':')[0];
		var min = time.split(':')[1];
		var sec = time.split(':')[2];
		return Number(hour*3600) + Number(min*60) + Number(sec);
	}
	
	OpenFrameAttribute = function () {
		var a1 = $("#p1");
		var sumRunTime = 0, sumMachTime = 0;
		S = 0; D = 0; G = 0;
		var ajaxParam = {
			url: '/iPlant_ajax',
			dataType: 'JSON',
			data: {
				//IFS: 'B1001'
				IFS: 'MES_R00115'
			}, 
			successCallBack: function (data) {
				
				//ET_STATE = 1 生产, 2 暂停 3 异常,  空或者0 = 停机
				var op = data.RESPONSE[0].RESPONSE_DATA;
				$.each(op, function (n, obj) {
					if (obj.ET_NM != null) {
						
						if( obj.ET_STATE == 1 || obj.ET_STATE == -1 || obj.ET_STATE == 5 ){ //生产中
							S++
						}
						if( obj.ET_STATE == 2 || obj.ET_STATE == 0 || obj.ET_STATE == -2 ){ // 暂停, 待机
							D++
						}
						if( obj.ET_STATE == 3 ){ // 故障
							G++
						}
					}
					
					obj['NAME'] = obj.ET_NM.slice(obj.ET_NM.length-2, obj.ET_NM.length)
					obj['ET_NM'] = obj.ET_NM.substring(obj.ET_NM.length-2, 2)
					obj.RUN_TIME = obj.RUN_TIME != "null"?obj.RUN_TIME:0
					obj.MACHINING_TIME = obj.MACHINING_TIME != "null"?obj.MACHINING_TIME:0;
					
					sumRunTime += Number(obj.RUN_TIME ) // 运行时间
					sumMachTime += Number(obj.MACHINING_TIME);
					// 计算稼移率 
					// 算法1 ---  (加工时间*本次产量)/设备运行时间*(本次加工数量/计划数量）*100
					
					// 算法2 ---  (累计 加工时间)/(运行时间*60)*100
					
					var a = Number(obj.MACHINING_TIME || 0) ; ///hsmCovMis( obj.MACHINING_TIME );  //加工时间
					var b = Number(obj.RUN_TIME)*60;  //运行时间
					var c = Number(obj.CURRENT_PRO_QTY);   //  本次产量
					var d = Number(obj.PLAN_PRO_QTY);  //计划数
					if( b == 0 ){
						//除数不能是0, 否则NAN
						obj['jyl'] = 0
					}else{
						obj['jyl'] = (a/b)*100
					}
					// }
					obj['jyl'] = obj['jyl'].toFixed(2)
					obj['jyl'] = obj['jyl'] + '%'
					
					
					obj.MACHINING_TIME = Math.floor((obj.MACHINING_TIME/60))
					
					
				});
				
				if( sumRunTime == 0 ){
					avg = 0 //平均稼动率
				}else{
					/// 加工时间/运行时间
					avg = (sumMachTime /(sumRunTime*60))*100; //平均稼动率
				}
				
				a(Math.floor(avg));  //稼移率
				  // 设备使用率
				var userate = S/op.length;
				userate = Math.floor(userate*100)
				if( isNaN(userate) ){
					userate = 0
				}
				$('#sps2').text(userate + "%")
				  
				// charts();

				//2020-6-20  调整主体部分设备信息显示区域布局及模板渲染
				if( op.length > 0 ){
					var myTemplate = Handlebars.compile($("#employeesInfoTemplate").html());
					var strHtml = myTemplate({ 'domData': op });
					a1.html(strHtml);
					op.forEach(function (item, index) {
						if (item.ET_PL) {
							// $('#p1 > div').find('img').eq(index).attr('src', '../../../../iTaurus/common/img/' + item.ET_PL)
						}
						//sbMsg   by:tang --- 模板语法, if 不能写表达式... 
						var str = ''
						if( item.ET_STATE == 1 || item.ET_STATE == -1 || item.ET_STATE == 5 ){
							str += `<button class="t-btn green"></button>
							<span style="color:#30CCC4;">开机</span>
							<button class="t-btn"></button>
							<span style="color:#30CCC4;">停机</span>
							<button class="t-btn"></button>
							<span style="color:#30CCC4;">异常</span>`
						}else if( item.ET_STATE == 2 || item.ET_STATE == 0 || item.ET_STATE == -2 ){
							str += `<button class="t-btn"></button>
							<span style="color:#30CCC4;">开机</span>
							<button class="t-btn yewllo"></button>
							<span style="color:#30CCC4;">停机</span>
							<button class="t-btn"></button>
							<span style="color:#30CCC4;">异常</span>`
						}else if( item.ET_STATE == 3 ){
							str += `<button class="t-btn"></button>
							<span style="color:#30CCC4;">开机</span>
							<button class="t-btn"></button>
							<span style="color:#30CCC4;">停机</span>
							<button class="t-btn red"></button>
							<span style="color:#30CCC4;">异常</span>`
							
							if( item.ERROR_MESSAGER == 'null' ){
								item.ERROR_MESSAGER = '异常处理中'
							}
							$('.boxs-item').eq(index).find('.error').show()
						}
						$('.sbMsg').eq(index).append(str)
					})
				}
				
			}
		}
		iplantAjaxRequest(ajaxParam);

	}
	OpenFrameAttribute();
	setInterval(function(){
		if( nowAjax >= maxAjax ){
			location.reload();
		}else{
			OpenFrameAttribute()
			++nowAjax;
		}
	}, 20*1000);

	function a(avg) {
		$("#sps").html(avg + "%");   // 嫁移率
		$('#pro').progressbar({
			value: avg,
			height: 20,
			color: 'red'
		});
		$(".progressbar-value .progressbar-text").css("background-color", "#67D3C9");
	}

	var m = 0;
})();

Handlebars.registerHelper('ifState', function(value, options) {
  if(value === 0) {
    return options.fn(this);
  }
  return options.inverse(this);
});